package com.woddp.flutterplugin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import java.util.UUID;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlutterPlugin */
public class FlutterPlugin implements MethodCallHandler {

  private Activity activity;
  FlutterPlugin(Activity activity){
    this.activity=activity;
  }
  /** Plugin registration. */
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_plugin");
    channel.setMethodCallHandler(new FlutterPlugin(registrar.activity()));
  }

  public void  alert(){
    AlertDialog.Builder builder=new AlertDialog.Builder(activity);
    builder.setTitle("下载提示");
    builder.setMessage("你没有安卓迅雷，是否安卓迅雷");
    builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        String link = "http://m.down.sandai.net/mobile/OfficialSite_MobileThunder2.apk";
        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
        intent.addCategory("android.intent.category.DEFAULT");
        activity.startActivity(intent);
      }});

    builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int which) {

      }});
    builder.create();                      // 在Activity中创建提示框
    builder.show();
  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("getPlatformVersion")) {
      final TelephonyManager tm = (TelephonyManager) activity.getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
      final String tmDevice, tmSerial, androidId;
      tmDevice = "" + tm.getDeviceId();
      tmSerial = "" + tm.getSimSerialNumber();
      androidId = "" + android.provider.Settings.Secure.getString(activity.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
      UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
      String deviceId = deviceUuid.toString();
      result.success("Android " + deviceId);
    }
    if (call.method.equals("show")) {
      String msg = call.argument("msg").toString();
      Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
    }
    //下载
    if (call.method.equals("down")) {
      String link = call.argument("url").toString();
      result.success(link);
      try{
        String appPackageName ="com.xunlei.downloadprovider";
        ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        ClipData myClip;
        myClip = ClipData.newPlainText("text", link);
        cm.setPrimaryClip(myClip);

        PackageManager packageManager=activity.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(appPackageName);
        activity.startActivity(intent);
      }catch(Exception e){
//        Toast.makeText(activity, "没有安装", Toast.LENGTH_LONG).show();
        this.alert();
      }
    }
    //复制内容到文本
    if (call.method.equals("clip")) {
      String msg = call.argument("msg").toString();
      ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
      ClipData myClip;
      myClip = ClipData.newPlainText("text", msg);
      cm.setPrimaryClip(myClip);
    }
    //跳转
    if (call.method.equals("href")) {
      String pack = call.argument("msg").toString();
      result.success(pack);
      String appPackageName =pack;
      PackageManager packageManager=activity.getPackageManager();
      Intent intent = packageManager.getLaunchIntentForPackage(appPackageName);
      activity.startActivity(intent);
    }
  }
}
